<template>
	<view>
		<u-row gutter="16" justify="center" style="background: #1E88E5;">
			<u-col span="11">
				<u-search shape="square" bg-color="#fff" placeholder="输入访客人员姓名关键字" :action-style="actionStyle" v-model="keyword" style="margin: 48rpx 0 30rpx 0;" @search="search" @custom="search"></u-search>
			</u-col>
		</u-row>
		<scroll-view v-if="list.length>0" class="scroll" scroll-y="true">
			<u-checkbox-group class="checkbox-group" @change="checkboxGroupChange">
				<!-- <u-checkbox v-model="item.checked" v-for="(item, index) in list" :key="index" :name="item.id"
				:disabled="item.type==2 && isCheckbox(item.visitArea)?true:false"> -->
				<u-checkbox v-model="item.checked" v-for="(item, index) in list" :key="index" :name="item.id" label-disabled
				:disabled="item.type==2 && (!item.position || !nearAddrs.some(nearAddr => item.position.includes(nearAddr)))?true:false">
					<u-row gutter="16">
						<u-tag v-if="item.type==1" text="普" border-color="#FFECB2" bg-color="#FFECB2" color="#FEB500"></u-tag>
						<u-tag v-if="item.type==2" text="货" border-color="#DAEDFF" bg-color="#DAEDFF" color="#1976D2"></u-tag>
						<u-tag v-if="item.type==3" text="施" border-color="#AFECB2" bg-color="#AFECB2" color="#1EB500"></u-tag>
						<view class="head-title">{{item.visitor}}</view>
					</u-row>
					<u-row gutter="16" style="margin-top: 25rpx;">
						<u-col span="12">
							<view class="apply-text">手机号码：{{item.vphone}}</view>
						</u-col>
						<u-col span="12">
							<view class="apply-text">拜访事由：{{item.vreason}}</view>
						</u-col>
						<u-col span="12">
							<view class="apply-text">来访时间：{{item.visitTime}}</view>
						</u-col>
						<u-col v-if="item.type!=1" span="12">
							<view class="apply-text">车牌号：{{item.carNo}}</view>
						</u-col>
						<u-col v-if="item.type==2" span="12">
							<view class="apply-text">进入区域：
								<u-radio-group v-model="item.deptId">
									<u-radio v-for="(item1, index) in getVisitArea(item.visitArea)" :key="index" :name="item1.orgId">
										{{ item1.name }}
									</u-radio>
								</u-radio-group>
							</view>
						</u-col>
						<u-col v-if="item.type!=1" span="12">
							<view class="apply-text">最新位置：{{item.position}}</view>
						</u-col>
						<u-col span="12">
							<view class="apply-text">被访人：{{item.staff}}</view>
						</u-col>
					</u-row>
					<u-row gutter="16" style="margin-top: 20rpx;">
						<u-col span="12">
							<u-button plain @click="goDetail(item.id)" style="height: 60rpx;">查看详情</u-button>
						</u-col>
					</u-row>
				</u-checkbox>
			</u-checkbox-group>
		</scroll-view>
		<pages-empty v-else status="empty" tips="该条消息已审批过"></pages-empty>
		<u-row gutter="32" class="foot">
			<u-col span="3">
				<u-button type="primary"  @click="dayAppr">今日审批</u-button>
			</u-col>
			<u-col span="3">
				<u-button type="primary" v-if="list.find(item => item.type == 2)" @click="toPlace">车位情况</u-button>
			</u-col>
			<u-col span="3">
				<u-button type="error" @click="reject">拒绝</u-button>
			</u-col>
			<u-col span="3">
				<u-button type="primary" @click="agree">同意</u-button>
			</u-col>
		</u-row>
		<!-- <view v-if="carNum>18" class="top-msg red">厂内车位不足</view> -->
		<u-modal v-model="showPlace" title="各仓库车位" :show-confirm-button="false" mask-close-able>
			<view class="slot-content">
				<u-row gutter="32" v-for="(item, index) in placeList" :key="index" :name="item.orgId" style="padding: 6rpx 0;">
					<u-col span="6">
						{{ item.name }}
					</u-col>
					<u-col span="6">
						{{item.maxStopCount-item.data>0?'剩余'+(item.maxStopCount-item.data)+'车位':'车位已满'}}
					</u-col>
				</u-row>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nearAddrs: ['台光电子','倪家浜路','金茂路','华杨南路','超英','杜家','大观溇路','305乡道'],
				Audit: '',
				Staff: '',
				placeList: [],
				showPlace: false,
				carNum: 0,
				checkedAll: false,
				actionStyle: {
					color: '#fff'
				},
				keyword: '',
				leaderId: '', // 被访人Id
				ids: [], // 勾选的人员id
				list: [],
				allList: [],
				intervalId: null // 新增属性以保存定时器ID
			}
		},
		onLoad(options) {
			this.leaderId = options.leaderId
			this.Audit = options.Audit
			if(options.Staff) {
				this.Staff = options.Staff
			}
			this.getApplicants()
			this.getVisTypeEnterCount()
		},
		mounted() {
			// 设置定时器每隔一定时间刷新二维码
			this.intervalId = setInterval(() => {
				this.getApplicants()
				this.getVisTypeEnterCount()
			}, 60 * 1000 * 5); // 每5分钟刷新一次，可以根据需要调整
		},
		beforeDestroy() {
			// 清除定时器
			if (this.intervalId) {
				clearInterval(this.intervalId);
			}
		},
		methods: {
			dayAppr() {
				uni.navigateTo({
					url: '/pages/wxmsg/dayAppr?leaderid='+this.leaderId
				})
			},
			getVisitArea(str) {
				const visitArea = str.split(',')
				return this.placeList.filter(item => visitArea.includes(item.name))
			},
			toPlace() {
				uni.navigateTo({
					url: '/pages/wxmsg/placeTabs'
				})
			},
			isCheckbox(str) {
				const visitArea = str.split(',')
				const arr = this.placeList.filter(item => visitArea.includes(item.name))
				return visitArea.some(name => {
				  const item = arr.find(obj => obj.name === name);
				  return item && item.maxStopCount <= item.data;
				});
			},
			getVisTypeEnterCount() {
				this.$u.get('/visitlogextend/vistypeentercount').then(res => {
					if(res.code==200){
						this.placeList = res.result
						// const found = res.result.find(item => item.type == 2)
						// if (found) {
						// 	this.carNum =  found.count
						// }
					}else{
						this.$u.toast('获取车辆数失败',res.message)
					}	
				}).catch(err => {
					this.$u.toast('获取车辆数异常',err)
				})
			},
			goDetail(id) {
				uni.navigateTo({
					url: '/pages/detail/detail?id='+id
				});
			},
			reject() {
				if(this.ids.length==0){
					return this.$u.toast('至少勾选一条')	
				}
				this.$u.post('/visitlogextend/batch',{status:20,ids:this.ids}).then(res => {
					if(res.code==200){
						this.getApplicants()
						this.$u.toast('拒绝成功')
					}else{
						this.$u.toast(res.message)
					}	
				}).catch(err => {
					this.$u.toast(err)
				})
			},
			agree() {
				if(this.ids.length==0){
					return this.$u.toast('至少勾选一条')	
				}
				const arr = this.list.filter(item => item.checked==true)
				const deptIds = []
				this.ids = []
				arr.map(val => {
					deptIds.push(val.deptId)
					this.ids.push(val.id)
				})
				this.$u.post('/visitlogextend/batch',{status:10,ids:this.ids,deptIds,Audit:this.Audit}).then(res => {
					if(res.code==200){
						this.getApplicants()
						this.$u.toast('同意成功')
					}else{
						this.$u.toast(res.message)
					}	
				}).catch(err => {
					this.$u.toast(err)
				})
			},
			getApplicants() {
				this.$u.post('/visitlog/page',{leaderId:this.leaderId,visitor:this.keyword,status:0,page:1,pageSize:1000000}).then(res => {
					if(res.code==200){
						this.checkedAll = false
						res.result.items.map(val => {
							val.checked = false
						})
						if(this.Staff!='') {
							const arr = res.result.items.filter(item => item.staffConfirm=='0')
							this.list = arr
							this.allList = arr
						}else {
							const arr = res.result.items.filter(item => item.staffConfirm!='0')
							this.list = arr
							this.allList = arr
						}
						// this.list = res.result.items
						// this.allList = res.result.items
					}else{
						this.$u.toast('请求失败',res.message)
					}	
				}).catch(err => {
					this.$u.toast('请求异常',err)
				})
			},
			search() {
				this.list = this.allList.filter(item => item.visitor.includes(this.keyword))
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				this.ids = []
				if(e.value) {
					this.list.map(val => {
						val.checked = true
						this.ids.push(val.id)
					})
				}else{
					this.list.map(val => {
						val.checked = false
					})
					this.ids = []
				}
			},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				this.ids = e
				if(this.list.length==this.ids.length){
					this.checkedAll = true
				}else{
					this.checkedAll = false
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #fff;
	}
	.scroll {
		height: calc(100vh - var(--window-top) - var(--window-bottom) - 242rpx);
		width: 100%;
	}
	
	/deep/ .u-checkbox-group {
		display: flex;
		flex-direction: column;
		.u-checkbox {
			align-items: flex-start;
			padding-top: 60rpx;
			.u-checkbox__icon-wrap {
				margin: 20rpx 20rpx 0 30rpx;
			}
			
			.u-checkbox__label {
				border-bottom: 1rpx solid #F5F5F5;
				padding-bottom: 28rpx;
				.u-tag {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 72rpx;
					height: 72rpx;
					border-radius: 100%;
					font-size: 40rpx;
					font-family: PingFang SC;
					font-weight: bold;
				}
			}
		}
	}
	
	.head-title {
		margin-left: 20rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #000000;
	}
	
	.apply-text {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #434343;
		margin-top: 5rpx;
	}
	
	.foot {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 112rpx;
		border-top: 1rpx solid #F5F5F5;
		background: #fff;
	}
	
	.top-msg {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		top: var(--window-top);
		width: 100%;
		height: 88rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		z-index: 999;
	}
	.top-msg.green {
		background: #19BE6B;
	}
	.top-msg.red {
		background: #FA3534;
	}
	
	.slot-content {
		padding: 30rpx;
		font-size: 15px;
	}
</style>
